﻿//***********************************************************************
// * Module:   MsjBaSqslbapxdwHandler
// * 作者:  XWG
// * Modified: 2013-5-29
// * Purpose: Definition of the Class MsjBaSqslbapxdwHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;

namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// MsjBaSqslbapxdwHandler 的摘要说明
    /// </summary>
    public class MsjBaSqslbapxdwHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                MsjBaSqslbapxdw mMsjBaSqslbapxdw = new MsjBaSqslbapxdw();
                mMsjBaSqslbapxdw.Sqid = Guid.NewGuid().ToString(); //主键
                mMsjBaSqslbapxdw.Sqrxm = context.Request.Form["texSqrxm"]; //申请人名称
                mMsjBaSqslbapxdw.Sqrdz = context.Request.Form["texSqrdz"]; //申请人地址
                mMsjBaSqslbapxdw.Sqlxr = context.Request.Form["texSqlxr"]; //申请联系人
                mMsjBaSqslbapxdw.Mc = context.Request.Form["texMc"]; //名称
                mMsjBaSqslbapxdw.Dz = context.Request.Form["texDz"]; //地址
                mMsjBaSqslbapxdw.Xzyz = context.Request.Form["texXzyz"]; //校长院长
                mMsjBaSqslbapxdw.Xzlxdh = context.Request.Form["texXzlxdh"]; //校长联系电话
                mMsjBaSqslbapxdw.Fddbr = context.Request.Form["texFddbr"]; //法定代表人
                mMsjBaSqslbapxdw.Fddbrlxdh = context.Request.Form["texFddbrlxdh"]; //法定代表人联系电话
                mMsjBaSqslbapxdw.Szrs = Convert.ToInt32(context.Request.Form["texSzrs"]); //师资人数
                mMsjBaSqslbapxdw.Zzszrs = Convert.ToInt32(context.Request.Form["texZzszrs"]); //专职师资人数
                mMsjBaSqslbapxdw.Pxlx = context.Request.Form["texPxlx"]; //培训类型
                mMsjBaSqslbapxdw.Pygm = context.Request.Form["texPygm"]; //培养规模
                mMsjBaSqslbapxdw.Xymj = Convert.ToDouble(context.Request.Form["texXymj"]); //校园面积(平方米)
                mMsjBaSqslbapxdw.Xsmj = Convert.ToDouble(context.Request.Form["texXsmj"]); //校舍面积(平方米)
                mMsjBaSqslbapxdw.Zczj = Convert.ToDouble(context.Request.Form["texZczj"]); //注册资金(万元)
                mMsjBaSqslbapxdw.Tzlx = context.Request.Form["texTzlx"]; //投资类型
                mMsjBaSqslbapxdw.Shijgajgyj = context.Request.Form["texShijgajgyj"]; //市级人民政府公安机关意见
                mMsjBaSqslbapxdw.Shijspr = context.Request.Form["texShijspr"]; //市级审批人
                mMsjBaSqslbapxdw.Shijspsj = context.Request.Form["texShijspsj"]; //市级审批时间
                mMsjBaSqslbapxdw.Sjgajgyj = context.Request.Form["texSjgajgyj"]; //省级人民政府公安机关意见
                mMsjBaSqslbapxdw.Sjspr = context.Request.Form["texSjspr"]; //省级审批人
                mMsjBaSqslbapxdw.Sjspsj = context.Request.Form["texSjspsj"]; //省级审批时间 
                mMsjBaSqslbapxdw.Cjr = context.Session["UserID"].ToString(); //创建人(人员ID)
                //mMsjBaSqslbapxdw.Cjrdw = context.Request.Form["texCjrdw"]; //创建单位(单位ID)  
                mMsjBaSqslbapxdw.Enabled = Enabled.TRUE;
                mMsjBaSqslbapxdw.Movesign = "0";
                mResultModel = MsjBaSqslbapxdwBLL.Insert(mMsjBaSqslbapxdw, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            string paramKey = context.Request.Params["id"];
            try
            {
                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }

                MsjBaSqslbapxdw mMsjBaSqslbapxdw = new MsjBaSqslbapxdw();
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mResultModel = MsjBaSqslbapxdwBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel));  //序列化后返回
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            MsjBaSqslbapxdw mMsjBaSqslbapxdw = new MsjBaSqslbapxdw();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mMsjBaSqslbapxdw = MsjBaSqslbapxdwBLL.Load(paramKey); //根据键值获取实体信息
                if (mMsjBaSqslbapxdw != null)
                {
                    mMsjBaSqslbapxdw.Sqrxm = context.Request.Form["texSqrxm"]; //申请人名称
                    mMsjBaSqslbapxdw.Sqrdz = context.Request.Form["texSqrdz"]; //申请人地址
                    mMsjBaSqslbapxdw.Sqlxr = context.Request.Form["texSqlxr"]; //申请联系人
                    mMsjBaSqslbapxdw.Mc = context.Request.Form["texMc"]; //名称
                    mMsjBaSqslbapxdw.Dz = context.Request.Form["texDz"]; //地址
                    mMsjBaSqslbapxdw.Xzyz = context.Request.Form["texXzyz"]; //校长院长
                    mMsjBaSqslbapxdw.Xzlxdh = context.Request.Form["texXzlxdh"]; //校长联系电话
                    mMsjBaSqslbapxdw.Fddbr = context.Request.Form["texFddbr"]; //法定代表人
                    mMsjBaSqslbapxdw.Fddbrlxdh = context.Request.Form["texFddbrlxdh"]; //法定代表人联系电话
                    mMsjBaSqslbapxdw.Szrs = Convert.ToInt32(context.Request.Form["texSzrs"]); //师资人数
                    mMsjBaSqslbapxdw.Zzszrs = Convert.ToInt32(context.Request.Form["texZzszrs"]); //专职师资人数
                    mMsjBaSqslbapxdw.Pxlx = context.Request.Form["texPxlx"]; //培训类型
                    mMsjBaSqslbapxdw.Pygm = context.Request.Form["texPygm"]; //培养规模
                    mMsjBaSqslbapxdw.Xymj = Convert.ToDouble(context.Request.Form["texXymj"]); //校园面积(平方米)
                    mMsjBaSqslbapxdw.Xsmj = Convert.ToDouble(context.Request.Form["texXsmj"]); //校舍面积(平方米)
                    mMsjBaSqslbapxdw.Zczj = Convert.ToDouble(context.Request.Form["texZczj"]); //注册资金(万元)
                    mMsjBaSqslbapxdw.Tzlx = context.Request.Form["texTzlx"]; //投资类型
                    mMsjBaSqslbapxdw.Shijgajgyj = context.Request.Form["texShijgajgyj"]; //市级人民政府公安机关意见
                    mMsjBaSqslbapxdw.Shijspr = context.Request.Form["texShijspr"]; //市级审批人
                    mMsjBaSqslbapxdw.Shijspsj = context.Request.Form["texShijspsj"]; //市级审批时间
                    mMsjBaSqslbapxdw.Sjgajgyj = context.Request.Form["texSjgajgyj"]; //省级人民政府公安机关意见
                    mMsjBaSqslbapxdw.Sjspr = context.Request.Form["texSjspr"]; //省级审批人
                    mMsjBaSqslbapxdw.Sjspsj = context.Request.Form["texSjspsj"]; //省级审批时间

                    mMsjBaSqslbapxdw.Xgr = context.Session["UserID"].ToString(); //修改人(人员ID)
                    // mMsjBaSqslbapxdw.Xgrdw = context.Request.Form["texXgrdw"]; //修改人单位(单位ID)
                    mMsjBaSqslbapxdw.Xgsj = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //修改时间(2013-05-28 09:13:01) 
                    mMsjBaSqslbapxdw.Movesign = "0"; //是否传输（1已，0未）

                    mResultModel = MsjBaSqslbapxdwBLL.Update(mMsjBaSqslbapxdw, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;
                }

                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = string.Empty;
            try
            {
                MsjBaSqslbapxdw mMsjBaSqslbapxdw = new MsjBaSqslbapxdw(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mMsjBaSqslbapxdw = MsjBaSqslbapxdwBLL.Load(paramKey); //根据键值获取管理员
                    if (mMsjBaSqslbapxdw != null)
                    {
                        json = JsonConvert.SerializeObject(mMsjBaSqslbapxdw);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }

            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string sqlWhere = " enabled=1 ";
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
            {
                return;
            }
            string mc = context.Request.Form["texMc"];
            string fddbr = context.Request.Form["texFddbr"];
            string fddbrlxdh = context.Request.Form["texFddbrlxdh"];

            SqlConditionList sqllist = new SqlConditionList();

            if (!string.IsNullOrEmpty(mc)) ////姓名
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjBaSqslbapxdwConst.Columns.Mc, '%' + mc + '%'), SqlLogicOper.AND_LOGIC);
            }

            if (!string.IsNullOrEmpty(fddbr)) ////法定代表人
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjBaSqslbapxdwConst.Columns.Fddbr, '%' + fddbr + '%'), SqlLogicOper.AND_LOGIC);
            }

            if (!string.IsNullOrEmpty(fddbrlxdh)) ////姓名
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjBaSqslbapxdwConst.Columns.Fddbrlxdh, fddbrlxdh), SqlLogicOper.AND_LOGIC);
            }

            if (string.IsNullOrEmpty(sqllist.ToSql()))
            {
                sqlWhere = "where" + sqlWhere;
            }
            else
            {
                sqlWhere = sqllist.ToSql() + " and " + sqlWhere;
            }

            DataTable dtMsjBaSqslbapxdw = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();
            dtMsjBaSqslbapxdw = q.GetRecordByPage("*", sqlWhere, string.Empty, intPageSize, intPageIndex, MsjBaSqslbapxdwConst.TableName, MsjBaSqslbapxdwConst.FieldKey, string.Empty).Tables[0];
            if (dtMsjBaSqslbapxdw != null && dtMsjBaSqslbapxdw.Rows.Count > 0)
            {
                for (int i = 0; i < dtMsjBaSqslbapxdw.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtMsjBaSqslbapxdw.Columns[i].Caption;
                    list.Add(hm);
                }

                tm.Header = list;
                tm.data = dtMsjBaSqslbapxdw;
                tm.totalCount = Convert.ToString((int)q.RowCount);
            }

            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }

        protected void QueryGtRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";

            QueryInfo info = new QueryInfo();
            QueryByPage page = new QueryByPage();
            info.FieldKey = MsjBaSqslbapxdwConst.FieldKey;
            info.PageIndex = 1;
            info.PageSize = 0;
            info.QTableName = MsjBaSqslbapxdwConst.TableName;
            info.SelectFields = "*";

            info.Where = string.Empty;
            DataSet ds = page.GetRecordByPage(info);
            DataTable table = new DataTable();

            if (ds.Tables.Count > 0)
            {
                table = page.GetRecordByPage(info).Tables[0];
            }
            string json = JsonConvert.SerializeObject(table);
            context.Response.Write(json);
        }
        #endregion
    }
}


